﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for TblOption
    /// </summary>
    public class TblOptionManagerBase
    {
        public TblOptionManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void InsertUpdate(TblOptionEntity item)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", item.Id));
            ls.Add(new SqlParameter("@Name", item.Name));
            ls.Add(new SqlParameter("@Description", item.Description));
            ls.Add(new SqlParameter("@Value", item.Value));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblOption", sqlparam);
        }
        public void InsertUpdate(int Id, string Name, string Description, string Value)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@Name", Name));
            ls.Add(new SqlParameter("@Description", Description));
            ls.Add(new SqlParameter("@Value", Value));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblOption", sqlparam);
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblOption", sqlparam);
        }
        public TblOptionEntity GetTblOptionEntity(DataRow dr)
        {
            TblOptionEntity item = new TblOptionEntity();
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();
            if (dr.Table.Columns.Contains("Name"))
                item.Name = dr["Name"].ToString();
            if (dr.Table.Columns.Contains("Description"))
                item.Description = dr["Description"].ToString();
            if (dr.Table.Columns.Contains("Value"))
                item.Value = dr["Value"].ToString();
            return item;
        }
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblOption", sqlparam);
        }
        public TblOptionEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblOption", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                return GetTblOptionEntity(dt.Rows[0]);
            return null;
        }
        public TblOptionEntity SelectOneByName(string Name)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Name", Name));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblOption", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                return GetTblOptionEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblOptionsAll", null);
        }
        public List<TblOptionEntity> SelectAll()
        {
            List<TblOptionEntity> lsresult = new List<TblOptionEntity>();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblOptionsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblOptionEntity(dt.Rows[i]));
            return lsresult;
        }

       
    }
}

